﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XmlCreate
{
    public partial class LOGROUPForm : Form
    {
        public LOGROUPForm(string _descFrombase, string _nameFrombase, IODEFLOGROUP _IOGROUP)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            desctext.Text = _descFrombase;
            nametext.Text = _nameFrombase;
            if (!string.IsNullOrEmpty(_descFrombase))
            {
                desctext.ReadOnly = true;
            }

            if (!string.IsNullOrEmpty(_nameFrombase))
            {
                nametext.ReadOnly = true;
            }
            if (_IOGROUP != null && _IOGROUP.IOGROUP != null && _IOGROUP.name != null && _IOGROUP.name != null)
            {
                desctext.Text = _IOGROUP.desc;
                nametext.Text = _IOGROUP.name;
                for (int i = 0; i < _IOGROUP.IOGROUP.Count; i++)
                {
                    ioggroupDgv.Rows[i].Cells["name"].Value = _IOGROUP.IOGROUP[i].name;
                    ioggroupDgv.Rows[i].Cells["desc"].Value = _IOGROUP.IOGROUP[i].desc;
                }
            }

        }

        public int isDetail= 0;
        List<IODEFIOGROUP> iODEFLOGROUPIOGROUPs = new List<IODEFIOGROUP>();
        public IODEFLOGROUP iODEFLOGROUP = new IODEFLOGROUP();
        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ioggroupDgv.Columns[e.ColumnIndex].Name == "addTag" && e.RowIndex >= 0)
            {

                    IOGROUPForm iOGROUP = new IOGROUPForm
                (
                   ioggroupDgv.CurrentRow.Cells[1].Value.ToString(),
                    ioggroupDgv.CurrentRow.Cells[0].Value.ToString(), iODEFLOGROUPIOGROUPs.Count == 0 ? null : iODEFLOGROUPIOGROUPs[e.RowIndex]
                );
                if (iOGROUP.ShowDialog() == DialogResult.OK)
                {
                    iODEFLOGROUPIOGROUPs.Add(iOGROUP.IOGROUP);
                    isDetail += 1;
                }
            }
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Okbtn_Click(object sender, EventArgs e)
        {
            if (isDetail ==0 && ioggroupDgv.Rows.Count > 1)
            {
                for (int i = 0; i < ioggroupDgv.Rows.Count-1; i++)
                {
                    iODEFLOGROUPIOGROUPs.Add(new IODEFIOGROUP { 
                        desc = ioggroupDgv.Rows[i].Cells[1].Value.ToString(),
                        name = ioggroupDgv.Rows[i].Cells[0].Value.ToString()
                    });
                }
            }
            iODEFLOGROUP.desc = desctext.Text;
            iODEFLOGROUP.name = nametext.Text;
            iODEFLOGROUP.IOGROUP = iODEFLOGROUPIOGROUPs;
            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
